package product;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextField;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.awt.SystemColor;

import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;

import java.awt.Font;

public class ProductInput extends JInternalFrame {
	ProductMain4 main;
	int totEa = 0;
	boolean flag = true;
	DefaultTableModel model;
	DecimalFormat df = new DecimalFormat("#,###,###");

	private JPanel contentPane;
	JTextField textCode;
	private JTextField textName;
	private JTextField textPrice;
	private JTextField textVolume;
	private JButton btnCheck;
	private JButton btnSave;
	private JTextField textMember_id;
	private JButton btnNewButton;
	private JLabel lblstate;
	private JLabel lblEa;
	private JButton btnAvailableProductList;
	private JScrollPane scrollPane;
	private JTable table;
	private JPanel panel;
	private JButton btnNewButton_1;
	private JTextField textField;
	private JPanel panel_1;
	private JLabel label_4;
	private JLabel label_5;
	private JLabel lblEmployeeId;
	private JLabel label_7;
	private JLabel label_8;
	private JPanel panel_2;
	private JLabel lblProductInput;
	private JLabel lblProductInput_1;
	private JPanel panel_3;
	private JLabel lblProductSearch;
	private JLabel label;

	public ProductInput(ProductMain4 main) {
		super("ProductInput", false,true,false,true);
		this.main = main;
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameActivated(InternalFrameEvent e) {
				textCode.setText("");
				textMember_id.setText("");
				textName.setText("");
				textPrice.setText("");
				textVolume.setText("");
				lblstate.setText("");

			}
		});

		setBounds(100, 100, 600, 447);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblstate());
		contentPane.add(getLabel_1());
		contentPane.add(getBtnAvailableProductList());
		contentPane.add(getScrollPane());
		contentPane.add(getPanel());
		contentPane.add(getPanel_1());
		contentPane.add(getPanel_2());
		contentPane.add(getPanel_3());
		contentPane.add(getLabel());
	}

	public void save() {
		try {
//			OracleJdbc o = new OracleJdbc();
//			main.conn=o.conn;
			
			String sql = "insert into product values (product_num.nextval, ?, ?, sysdate, ?)";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textCode.getText());
			pstmt.setString(2, textVolume.getText());
			pstmt.setString(3, textMember_id.getText());
			pstmt.executeUpdate();
			lblstate.setText("생산하였습니다.");
			
			possibilitysearch();
			if (main.tList != null) {
				main.tList.searchData();
			}
			
			ProStock ps = new ProStock();
			ps.add(textCode.getText(), Integer.parseInt(textVolume.getText()));

			textCode.setText("");
			textMember_id.setText("");
			textName.setText("");
			textPrice.setText("");
			textVolume.setText("");
			lblEa.setText("");
						
			pstmt.close();
//			main.conn.close();

		} catch (Exception ee) {
		}
	}

	public void eaSearch() {
		totEa = 0;
		try {
//			OracleJdbc o = new OracleJdbc();
//			main.conn=o.conn;
			
			String sql = "select ea from delivery_stock where code=?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textCode.getText());

			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				totEa = rs.getInt(1);
				lblEa.setText("생산 가능량 :" + df.format(totEa));
			} else {
				lblEa.setText("생산 가능량 :" + df.format(totEa));
			}

			rs.close();
			pstmt.close();
//			main.conn.close();
		} catch (Exception e) {
		}
	}

	public void possibilitysearch() {
		model.setRowCount(0);
		if (textField.getText().equals("")) {
			try {
//				OracleJdbc o = new OracleJdbc();
//				main.conn=o.conn;
				
				String sql = "select code, pro_nm, ea from delivery_stock inner join code on code = pro_code order by code";
				PreparedStatement pstmt = main.conn.prepareStatement(sql);
				ResultSet rs = pstmt.executeQuery();

				while (rs.next()) {
					Vector<String> vv = new Vector<String>();
					vv.add(rs.getString(1));
					vv.add(rs.getString(2));
					vv.add(rs.getString(3));

					model.addRow(vv);
				}

				rs.close();
				pstmt.close();
//				main.conn.close();
				
			} catch (Exception e) {
			}
		} else {
			try {
//				OracleJdbc o = new OracleJdbc();
//				main.conn=o.conn;
				
				String sql = "select code, pro_nm, ea from delivery_stock inner join code on code = pro_code where code like ? or pro_nm like ? order by code";
				PreparedStatement pstmt = main.conn.prepareStatement(sql);
				pstmt.setString(1, "%" + textField.getText() + "%");
				pstmt.setString(2, "%" + textField.getText() + "%");
				ResultSet rs = pstmt.executeQuery();

				while (rs.next()) {
					Vector<String> vv = new Vector<String>();
					vv.add(rs.getString(1));
					vv.add(rs.getString(2));
					vv.add(rs.getString(3));

					model.addRow(vv);
				}

				rs.close();
				pstmt.close();
//				main.conn.close();
			} catch (Exception e) {
			}
		}
	}

	public void codeSearch() {
		lblstate.setText("");
		try {
//			OracleJdbc o = new OracleJdbc();
//			main.conn=o.conn;
			
			String sql = "select pro_code, pro_nm, pro_price "
					+ "from code where pro_code=?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textCode.getText());
			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				String code = rs.getString(1);
				String name = rs.getString(2);
				String price = rs.getString(3);

				textName.setText(name);
				textPrice.setText(df.format(Integer.parseInt(price)) + "원");
				eaSearch();
			} else {
				lblstate.setText("등록된 코드가 없습니다.");
			}
			rs.close();
			pstmt.close();
//			main.conn.close();
			
		} catch (Exception ee) {
		}
	}

	private JTextField getTextCode() {
		if (textCode == null) {
			textCode = new JTextField();
			textCode.setBorder(new LineBorder(Color.GRAY));
			textCode.setBounds(145, 19, 202, 30);
			textCode.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					lblstate.setText("");
				}
			});
			textCode.setColumns(10);
		}
		return textCode;
	}

	private JTextField getTextName() {
		if (textName == null) {
			textName = new JTextField();
			textName.setBorder(new LineBorder(Color.GRAY));
			textName.setBounds(145, 59, 202, 30);
			textName.setEnabled(false);
			textName.setColumns(10);
		}
		return textName;
	}

	private JTextField getTextPrice() {
		if (textPrice == null) {
			textPrice = new JTextField();
			textPrice.setBorder(new LineBorder(Color.GRAY));
			textPrice.setBounds(145, 99, 202, 30);
			textPrice.setEnabled(false);
			textPrice.setHorizontalAlignment(SwingConstants.RIGHT);
			textPrice.setColumns(10);
		}
		return textPrice;
	}

	private JTextField getTextVolume() {
		if (textVolume == null) {
			textVolume = new JTextField();
			textVolume.setBorder(new LineBorder(Color.GRAY));
			textVolume.setBounds(145, 139, 202, 30);
			textVolume.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					try {
						lblstate.setText("");
						String a = df.format(Integer.parseInt(textVolume
								.getText()));
					} catch (Exception ee) {
						textVolume.setText("");
						lblstate.setText("수량엔 숫자만 입력해주세요.");
					}
				}
			});

			textVolume.setHorizontalAlignment(SwingConstants.RIGHT);
			textVolume.setColumns(10);
		}
		return textVolume;
	}

	private JButton getBtnCheck() {
		if (btnCheck == null) {
			btnCheck = new JButton("Search");
			btnCheck.setBorder(new LineBorder(Color.GRAY));
			btnCheck.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			btnCheck.setBounds(374, 19, 100, 30);
			btnCheck.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					codeSearch();
				}
			});
		}
		return btnCheck;
	}

	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton("Save");
			btnSave.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			btnSave.setBorder(new LineBorder(Color.GRAY));
			btnSave.setBounds(247, 227, 100, 30);
			btnSave.setEnabled(false);
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (Integer.parseInt(textVolume.getText()) <= totEa
							&& Integer.parseInt(textVolume.getText()) != 0
							&& Integer.parseInt(textVolume.getText()) > 0) {
						save();
						btnSave.setEnabled(false);
					} else {
						lblstate.setText("재고가 부족합니다.");
					}
				}
			});
		}
		return btnSave;
	}

	private JTextField getTextMember_id() {
		if (textMember_id == null) {
			textMember_id = new JTextField();
			textMember_id.setBorder(new LineBorder(Color.GRAY));
			textMember_id.setBounds(145, 179, 202, 30);
			textMember_id.setColumns(10);
		}
		return textMember_id;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Search");
			btnNewButton.setBorder(new LineBorder(Color.GRAY));
			btnNewButton.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			btnNewButton.setBounds(374, 179, 100, 30);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
//						OracleJdbc o = new OracleJdbc();
//						main.conn=o.conn;
						
						boolean flag = true;
						Statement st = main.conn.createStatement();

						String sql = "select irum from memberdata where memberid='"
								+ textMember_id.getText() + "'";
						ResultSet rs = st.executeQuery(sql);

						while (rs.next()) {
							String name = rs.getString(1);

							lblstate.setText(name + "님 확인되셨습니다.");
							btnSave.setEnabled(true);
							flag = false;
						}// while---------

						if (flag) {
							lblstate.setText("해당 아이디가 없습니다.");
							btnSave.setEnabled(false);
						}
						rs.close();
						st.close();
//						main.conn.close();

					} catch (Exception ee) {
					}
				}
			});
		}
		return btnNewButton;
	}

	private JLabel getLblstate() {
		if (lblstate == null) {
			lblstate = new JLabel("");
			lblstate.setBorder(new LineBorder(Color.GRAY, 2));
			lblstate.setForeground(new Color(255, 0, 0));
			lblstate.setOpaque(true);
			lblstate.setBackground(SystemColor.info);
			lblstate.setBounds(24, 369, 541, 32);
		}
		return lblstate;
	}

	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("");
			lblEa.setHorizontalAlignment(SwingConstants.CENTER);
			lblEa.setBorder(new LineBorder(Color.GRAY));
			lblEa.setOpaque(true);
			lblEa.setBackground(new Color(255, 192, 203));
			lblEa.setBounds(374, 139, 142, 30);
			lblEa.setForeground(new Color(255, 0, 0));
		}
		return lblEa;
	}

	private JButton getBtnAvailableProductList() {
		if (btnAvailableProductList == null) {
			btnAvailableProductList = new JButton("Available Product Search");
			btnAvailableProductList.setBorder(new LineBorder(Color.GRAY));
			btnAvailableProductList.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			btnAvailableProductList.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (flag) {
						possibilitysearch();
						setBounds(getX(), getY(), 1186, 447);
						flag = false;
					} else {
						setBounds(getX(), getY(), 599, 447);
						
						flag = true;
					}
				}
			});
			btnAvailableProductList.setBounds(341, 62, 224, 30);
		}
		return btnAvailableProductList;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(680, 157, 467, 199);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			Vector<String> v = new Vector<String>();
			v.add("Product Code");
			v.add("Product Name");
			v.add("Quantity");
			model = new DefaultTableModel(v, 0) {
				public boolean isCellEditable(int row, int column) {
					return false;
				}
			};
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					int q = table.getSelectedRow();

					textCode.setText((String) table.getValueAt(q, 0));
					codeSearch();
				}
			});
		}
		return table;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.activeCaption);
			panel.setBounds(680, 92, 467, 50);
			panel.setLayout(null);
			panel.add(getBtnNewButton_1());
			panel.add(getTextField());
		}
		return panel;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("Search");
			btnNewButton_1.setBorder(new LineBorder(Color.GRAY));
			btnNewButton_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			btnNewButton_1.setBounds(338, 9, 123, 30);
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					possibilitysearch();
				}
			});
		}
		return btnNewButton_1;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(15, 10, 311, 30);
			textField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						possibilitysearch();
					}
				}
			});
			textField.setColumns(10);
		}
		return textField;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBorder(new LineBorder(Color.GRAY, 2));
			panel_1.setBackground(SystemColor.inactiveCaptionBorder);
			panel_1.setBounds(24, 92, 541, 267);
			panel_1.setLayout(null);
			panel_1.add(getLabel_4());
			panel_1.add(getLabel_5());
			panel_1.add(getLblEmployeeId());
			panel_1.add(getLabel_7());
			panel_1.add(getLabel_8());
			panel_1.add(getTextMember_id());
			panel_1.add(getTextCode());
			panel_1.add(getTextName());
			panel_1.add(getTextPrice());
			panel_1.add(getTextVolume());
			panel_1.add(getBtnNewButton());
			panel_1.add(getBtnCheck());
			panel_1.add(getLblEa());
			panel_1.add(getBtnSave());
		}
		return panel_1;
	}
	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("Product Code");
			label_4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_4.setBounds(29, 19, 170, 30);
		}
		return label_4;
	}
	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("Product Name");
			label_5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_5.setBounds(29, 59, 170, 30);
		}
		return label_5;
	}
	private JLabel getLblEmployeeId() {
		if (lblEmployeeId == null) {
			lblEmployeeId = new JLabel("Employee ID");
			lblEmployeeId.setHorizontalAlignment(SwingConstants.LEFT);
			lblEmployeeId.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN, 15));
			lblEmployeeId.setBounds(29, 179, 170, 30);
		}
		return lblEmployeeId;
	}
	private JLabel getLabel_7() {
		if (label_7 == null) {
			label_7 = new JLabel("Quantity");
			label_7.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_7.setBounds(29, 139, 170, 30);
		}
		return label_7;
	}
	private JLabel getLabel_8() {
		if (label_8 == null) {
			label_8 = new JLabel("Unit Price");
			label_8.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			label_8.setBounds(29, 99, 170, 30);
		}
		return label_8;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setLayout(null);
			panel_2.setBorder(new LineBorder(Color.GRAY, 2));
			panel_2.setBounds(0, 0, 582, 50);
			panel_2.add(getLblProductInput());
		}
		return panel_2;
	}
	private JLabel getLblProductInput() {
		if (lblProductInput == null) {
			lblProductInput = new JLabel("Product Input");
			lblProductInput.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 40));
			lblProductInput.setBounds(164, 0, 325, 50);
		}
		return lblProductInput;
	}
	private JLabel getLabel_1() {
		if (lblProductInput_1 == null) {
			lblProductInput_1 = new JLabel("Product Information");
			lblProductInput_1.setForeground(Color.DARK_GRAY);
			lblProductInput_1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
			lblProductInput_1.setBounds(25, 60, 224, 30);
		}
		return lblProductInput_1;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setLayout(null);
			panel_3.setBorder(new LineBorder(Color.GRAY, 2));
			panel_3.setBounds(645, 0, 525, 50);
			panel_3.add(getLblProductSearch());
		}
		return panel_3;
	}
	private JLabel getLblProductSearch() {
		if (lblProductSearch == null) {
			lblProductSearch = new JLabel("Delivery Stock");
			lblProductSearch.setFont(new Font("Dialog", Font.PLAIN, 40));
			lblProductSearch.setBounds(124, 0, 325, 50);
		}
		return lblProductSearch;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("New label");
			label.setBorder(new LineBorder(Color.GRAY, 2));
			label.setBackground(Color.GRAY);
			label.setBounds(613, 60, 6, 322);
		}
		return label;
	}
}
